package org.oos.atom.elements;

import static org.oos.ApiUrlProvider.Base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.google.api.client.util.Key;

public class Category implements Serializable, Cloneable {

	private static final long serialVersionUID = -7202829770259265525L;

	public enum Type {
		CATEGORIES, ATTRIBUTES, TAGS, LISTS, PRIVACY, TRUSTED;
	}

	@Key("@term")
	private String term;

	@Key("@scheme")
	private String scheme;

	@Key("@label")
	private String label;

	@Key("oos:parent-category")
	private String parent;

	@Key("oos:frequency")
	private Integer frequency;

	@Override
	public Category clone() throws CloneNotSupportedException {
		return (Category) super.clone();
	}

	public String getTerm() {
		return term;
	}

	public String getScheme() {
		return scheme;
	}

	public String getLabel() {
		return label;
	}

	public String getParent() {
		return parent;
	}
	
	public Integer getFrequency() {
		return frequency;
	}

	public Category setTerm(String term) {
		this.term = term;
		return this;
	}

	public Category setScheme(String scheme) {
		this.scheme = scheme;
		return this;
	}

	public Category setLabel(String label) {
		this.label = label;
		return this;
	}

	public Category setParent(String parent) {
		this.parent = parent;
		return this;
	}
	
	public Category setFrequency(Integer frequency) {
		this.frequency = frequency;
		return this;
	}

	@Override
	public String toString() {
		return label;
	}

	public static List<Category> find(List<Category> categories, Type type) {
		List<Category> result = new ArrayList<Category>();
		if (categories != null) {
			for (Category category : categories) {
				if (category.getScheme().contains(type.name().toLowerCase())) {
					result.add(category);
				}
			}
		}
		return result;
	}
	
	public static List<Category> delete(List<Category> categories, Type type) {
		List<Category> result = new ArrayList<Category>();
		if (categories != null) {
			for (Category category : categories) {
				if (!category.getScheme().contains(type.name().toLowerCase())) {
					result.add(category);
				}
			}
		}
		return result;
	}
	
	public static Category create(Type type) {
		if (type.equals(Type.PRIVACY) || type.equals(Type.TRUSTED)) {
			Category category = new Category();
			String scheme = Base.getUrl(type.toString().toLowerCase()).toString();
			category.setScheme(scheme);
			return category;
		} else {
			return null;
		}
	}
	
	public static Category create(Type type, String slug) {
		if (type.equals(Type.TAGS) || type.equals(Type.LISTS)) {
			Category category = new Category();
			String scheme = Base.getUrl(type.toString().toLowerCase(), slug).toString();
			category.setScheme(scheme);
			return category;
		} else {
			return null;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Category) {
			Category other = (Category) obj;
			return this.scheme.equals(other.scheme) && this.term.equals(other.term);
		}
		return false;
	}
	
	
}
